package com.tianhao.luo.nouse.graph;

import java.util.HashMap;
import java.util.List;

/**
 * @author tianhao.luo
 * @date 2019/12/19 10:28
 */
public class RectangleCalculateFactory implements CalculateFactory {
    /**
     * 计算面积
     *
     * @param graph
     * @return
     */
    @Override
    public float calculateArea(Graph graph) {
        Rectangle rectangle = (Rectangle) graph;
        HashMap<Integer, List<Line>> map = rectangle.getMap();
        List<Line> lines = map.get(4);
        int length = lines.get(0).getLength();
        int height = 0;
        for (Line line : lines) {
            if (length != line.getLength()) {
                height = line.getLength();
                break;
            }
        }
        return 1F*height*length;
    }

    /**
     * 计算周长
     *
     * @param graph
     * @return
     */
    @Override
    public float calculatePerimeter(Graph graph) {
        Rectangle rectangle = (Rectangle) graph;
        HashMap<Integer, List<Line>> map = rectangle.getMap();
        List<Line> lines = map.get(4);
        int perimeter = 0;
        for (Line line : lines) {
            perimeter = perimeter +line.getLength();
        }
        return 1F*perimeter;
    }
}
